/*
 * HubSession.java
 *
 * Created on 28 de Junho de 2005, 22:49
 */

package jhub;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import jhub.conn.HubListener;
import jhub.dao.DAOFactory;
import jhub.text.DCRawCommand;

/**
 *
 * @author  botelhodaniel
 */
public class HubSession {
    
    private int sessionID;
    private Hub hub;
    private Date startDate;
    private Date end;
    private UserSessionList userSessionList;
    private HubListener listener;
    private ArrayList<DCRawCommand> receivedCommands;
    private ArrayList<DCRawCommand> sentCommands;
    
    private Thread listenerThread;
    
    protected void setup(Hub hub) {  
        this.hub = hub;
        this.receivedCommands = new ArrayList<DCRawCommand>();
        this.sentCommands = new ArrayList<DCRawCommand>();
        this.userSessionList = new UserSessionList();
        this.listener = new HubListener(this);
        listenerThread = new Thread(this.listener,"HubListener");
    }
    
    public HubSession(Hub hub) {  
        setup(hub);
    }
    
    public void startSession(){
        startDate = new Date();
         listenerThread.start();
         
        try {
            DAOFactory.getHubSessionDAO().startHubSession(this);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
         
    }
    
    public UserSessionList getUserSessionList(){
        return userSessionList;
    }

    public Hub getHub() {
        return hub;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public ArrayList<DCRawCommand> getReceivedCommands() {
        return receivedCommands;
    }

    public ArrayList<DCRawCommand> getSentCommands() {
        return sentCommands;
    }

    public void restartSession() {
        try {
            listener.stop();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        try {
            listenerThread.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        userSessionList.removeAll();
        listener = new HubListener(this);
        setup(hub);
        startSession();
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public int getSessionID() {
        return this.sessionID;
    }

    public void setSessionID(int sessionID) {
        this.sessionID = sessionID;
    }
    
    
}
